clear all
set more off
set maxvar 10000
version 13
set seed 123456789


* Set directory 
* -------------

	global directory ""

* Subfolder globals
* -----------------

	global figures			"${directory}/Figures"
	global greenberg		"${directory}/Greenberg"
	global simulations		"${directory}/Simulations"
	global spatial 			"${directory}/Spatial"
	global tables			"${directory}/Tables"

	
* Stata tex
* -----------
do "${tables}/stata-tex.do"		


* Figure 1: Log change computed between 2011 and 2018.
* ----------------------------------------------------
use "${directory}/ES_db.dta", clear  
preserve 
foreach var of varlist lnpopulation lnpecuniary_V1 lnNonpecu_people {
	replace `var' = - `var' if year == 2011 
	replace `var' = . if year == 2012 | year == 2013 | year == 2014 | year == 2015 | year == 2016 | year == 2017
}
collapse (sum) lnpopulation lnpecuniary_V1 lnNonpecu_people (mean) metropolitan, by(province local_municipality)
tempfile dvar 
save `dvar' 
use "${spatial}/sadb.dta", clear 
rename NAME_1 province
rename NAME_3 local_municipality
replace local_municipality = "KhĂ˘i-Ma" if local_municipality == "Khâi-Ma"
merge 1:1 province local_municipality using `dvar' 
format (lnpopulation) %12.2f 
gen x_centro_m = x_centro if metropolitan == 1 
gen y_centro_m = y_centro if metropolitan == 1 
gen Position = metropolitan 
replace Position = 2 if local_municipality =="Ekurhuleni"
gen name = local_municipality if metropolitan == 1 
replace name = "Cape Town" if name == "City of Cape Town"
replace name = "Johannesburg" if name == "City of Johannesburg"
replace name = "Tshwane" if name == "City of Tshwane"
replace name = "{bf:" + name + "}" if metropolitan == 1

spmap lnpopulation using "${spatial}/sacoord.dta", id(id) clm(custom) clb(-0.21 0 0.1 0.67) leglabel(name) fcolor(purple*.25  ebblue*1 ebblue*1.75) ndfcolor(white) legend(pos(4)) polygon(data("${spatial}/sa_provcoor") osize(thick)) point(xcoord( x_centro_m) ycoord( y_centro_m) fcolor(red)) label(xcoord( x_centro ) ycoord( y_centro ) label(name) size(*0.85 ..)  by(Position) pos(12 3) color(red red) length(23 23))
graph export "${figures}/Map_pop2011_2018.jpg", as(jpg) name("Graph") quality(100) replace

format (lnpecuniary_V1) %12.2f 
spmap lnpecuniary_V1 using "${spatial}/sacoord.dta", id(id) clm(custom) clb(-0.6 0 1.5) leglabel(name) fcolor(purple*.25  ebblue*1) ndfcolor(white) legend(pos(4)) polygon(data("${spatial}/sa_provcoor") osize(thick)) point(xcoord( x_centro_m) ycoord( y_centro_m) fcolor(red)) label(xcoord( x_centro ) ycoord( y_centro ) label(name) size(*0.85 ..)  by(Position) pos(12 3) color(red red) length(23 23))
graph export "${figures}/Map_pec2011_2018.jpg", as(jpg) name("Graph") quality(100) replace

format (lnNonpecu_people) %12.2f 
spmap lnNonpecu_people using "${spatial}/sacoord.dta", id(id) clm(custom) clb(-0.9 -0.25 0 0.38) leglabel(name) fcolor(purple*.75 purple*.25  ebblue*1) ndfcolor(white) legend(pos(4)) polygon(data("${spatial}/sa_provcoor") osize(thick)) point(xcoord( x_centro_m) ycoord( y_centro_m) fcolor(red)) label(xcoord( x_centro ) ycoord( y_centro ) label(name) size(*0.85 ..)  by(Position) pos(12 3) color(red red) length(23 23))
graph export "${figures}/Map_npec2011_2018.jpg", as(jpg) name("Graph") quality(100) replace
restore 


* Table 1: Baseline model, OLS & IV Estimates 
* -------------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

reghdfe lnpecuniary_V1 lnpopulation, cluster(id3) abs(year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es1) all
reghdfe lnpecuniary_V1 lnpopulation, cluster(id3) abs(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es2) all
reghdfe lnpecuniary_V1 lnpopulation, cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es3) all
ivreghdfe lnpecuniary_V1 (lnpopulation = IV) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es4) all
insert_into_file using "sample_table.csv", key(KP1) value(`e(rkf)') format(%5.2f)

reghdfe lnNonpecu_people lnpopulation, cluster(id3) abs(year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es5) all
reghdfe lnNonpecu_people lnpopulation, cluster(id3) abs(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es6) all
reghdfe lnNonpecu_people lnpopulation, cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es7) all
ivreghdfe lnNonpecu_people (lnpopulation = IV), cluster(id3) abs(id3 year) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es8) all
insert_into_file using "sample_table.csv", key(KP2) value(`e(rkf)') format(%5.2f)

table_from_tpl, t(TPL_Reg1.tex) r(sample_table.csv) o(T_Reg1.tex)


* Table 2: Test of the augmented shift-share, IV Estimates
* --------------------------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

ivreghdfe Education1  (lnpopulation=IV_d10_e20) , cluster(id3) abs(id3 year)
store_est_tpl using "$sample_table.csv", coef(lnpopulation) name(es1) all
insert_into_file using "$sample_table.csv", key(KP1) value(`e(rkf)') format(%5.2f)

ivreghdfe Education2  (lnpopulation=IV_d10_e20) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es2) all
insert_into_file using "sample_table.csv", key(KP2) value(`e(rkf)') format(%5.2f)

ivreghdfe Education3  (lnpopulation=IV_d10_e20) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es3) all
insert_into_file using "sample_table.csv", key(KP3) value(`e(rkf)') format(%5.2f)

ivreghdfe Education4  (lnpopulation=IV_d10_e20) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es4) all
insert_into_file using "sample_table.csv", key(KP4) value(`e(rkf)') format(%5.2f)

ivreghdfe Education1  (lnpopulation=IV_d10_e10) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es5) all
insert_into_file using "sample_table.csv", key(KP5) value(`e(rkf)') format(%5.2f)

ivreghdfe Education2  (lnpopulation=IV_d10_e10) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es6) all
insert_into_file using "sample_table.csv", key(KP6) value(`e(rkf)') format(%5.2f)

ivreghdfe Education3  (lnpopulation=IV_d10_e10) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es7) all
insert_into_file using "sample_table.csv", key(KP7) value(`e(rkf)') format(%5.2f)

ivreghdfe Education4  (lnpopulation=IV_d10_e10) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es8) all
insert_into_file using "sample_table.csv", key(KP8) value(`e(rkf)') format(%5.2f)

ivreghdfe Education1  (lnpopulation=IV_d10_e0) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es9) all
insert_into_file using "sample_table.csv", key(KP9) value(`e(rkf)') format(%5.2f)

ivreghdfe Education2  (lnpopulation=IV_d10_e0) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es10) all
insert_into_file using "sample_table.csv", key(KP10) value(`e(rkf)') format(%5.2f)

ivreghdfe Education3  (lnpopulation=IV_d10_e0) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es11) all
insert_into_file using "sample_table.csv", key(KP11) value(`e(rkf)') format(%5.2f)

ivreghdfe Education4  (lnpopulation=IV_d10_e0) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es12) all
insert_into_file using "sample_table.csv", key(KP12) value(`e(rkf)') format(%5.2f)

sum Education1 
insert_into_file using "sample_table.csv", key(mean1) value(`r(mean)') format(%5.2f)
sum Education2
insert_into_file using "sample_table.csv", key(mean2) value(`r(mean)') format(%5.2f)
sum Education3
insert_into_file using "sample_table.csv", key(mean3) value(`r(mean)') format(%5.2f)
sum Education4
insert_into_file using "sample_table.csv", key(mean4) value(`r(mean)') format(%5.2f)

table_from_tpl, t(TPL_Greenberg.tex) r(sample_table.csv) o(T_GreenbergTest.tex)


* Figure 2: Pecuniary crimes, IV Estimates
* ----------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

forvalues d = 0/20 {
	forvalues e=0/20 {
preserve
quietly ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d`d'_e`e') , cluster(id3) abs(id3 year)
quietly parmest,format(estimate min95 max95 %8.3f p %8.3f) list(,)   norestore 
keep parm stderr estimate p  
keep in 1
gen d = `d' - 10 
gen e = `e' - 10 
save  "${greenberg}/est_d`d'_e`e'.dta", replace 
restore	
	}
}
clear all 
cd "${greenberg}/"
forvalues d = 0/20 {
	forvalues e=0/20 {
append using  est_d`d'_e`e'  
	}
}
replace estimate=0 if p>0.05
replace estimate = round(estimate, 0.01)
centile estimate , c(5 10 25 50 75 90 95) 
twoway (contour estimate d e, graphregion(fcolor(white)) ccuts(-2.4(0.2)-1 0) crule(intensity) ecolor(dark)  zlabel(, format(%8.2f)  labsize(small))   yscale(axis(1) alt) xtitle({&theta}{subscript:2} < 0: Higher weight to educationnaly similar municipalities, size(small))  ytitle({&theta}{subscript:1} > 0: Higher weight to geographically distant municipalities, size(small)) title(`base' , )  heatmap ztitle("{&beta}") )  
graph export "{figures}/F_HM_Pecu.eps", as(eps) name("Graph") preview(on) replace 


* Table 3: Social network, IV Estimates
* ------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

ivreghdfe lnpecuniary_V1 (lnpopulation lnpopHerf = IV IVHerf) , abs(id3 year) cluster(id3) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es1) all
store_est_tpl using "sample_table.csv", coef(lnpopHerf) name(c1) all
insert_into_file using "sample_table.csv", key(KP1) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people (lnpopulation lnpopHerf = IV IVHerf) , abs(id3 year) cluster(id3) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es2) all
store_est_tpl using "sample_table.csv", coef(lnpopHerf) name(c2) all
insert_into_file using "sample_table.csv", key(KP2) value(`e(rkf)') format(%5.2f)

table_from_tpl, t(TPL_SC.tex) r(sample_table.csv) o(T_SC.tex)


* Table 4: Robustness on VCS data, IV Estimates
* ---------------------------------------------
use "${directory}/ES_Table4.dta", clear 
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

ivreghdfe Pecuniary (lnpopulation = Z) i.id3 i.year , cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es1) all
insert_into_file using "sample_table.csv", key(KP1) value(`e(rkf)') format(%5.2f)
boottest lnpopulation, cluster(id3) bootcluster(id3) weight(webb) level(95) seed(1) nograph 
return list 
insert_into_file using "sample_table.csv", key(P1) value(`r(p)') format(%5.2f)
sum Pecuniary, det 
insert_into_file using "sample_table.csv", key(M1) value(`r(mean)') format(%5.2f)

ivreghdfe Pecuniary (lnpopulation = Z) i.id3 i.year  if e_population == 1 , cluster(id3)  
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es2) all
insert_into_file using "sample_table.csv", key(KP2) value(`e(rkf)') format(%5.2f)
boottest lnpopulation, cluster(id3) bootcluster(id3) weight(webb) level(95) seed(1) nograph 
return list 
insert_into_file using "sample_table.csv", key(P2) value(`r(p)') format(%5.2f)
sum Pecuniary if e_population == 1, det 
insert_into_file using "sample_table.csv", key(M2) value(`r(mean)') format(%5.2f)

ivreghdfe Pecuniary (lnpopulation = Z) i.id3 i.year  if e_population != 1 , cluster(id3)   
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es3) all
insert_into_file using "sample_table.csv", key(KP3) value(`e(rkf)') format(%5.2f)
boottest lnpopulation, cluster(id3) bootcluster(id3) weight(webb) level(95) seed(1) nograph 
return list 
insert_into_file using "sample_table.csv", key(P3) value(`r(p)') format(%5.2f)
sum Pecuniary if e_population != 1, det 
insert_into_file using "sample_table.csv", key(M3) value(`r(mean)') format(%5.2f)

ivreghdfe Pecuniary (lnpopulation = Z) i.id3 i.year  if e_population == 1 & derp <= 4, cluster(id3) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es4) all
insert_into_file using "sample_table.csv", key(KP4) value(`e(rkf)') format(%5.2f)
boottest lnpopulation, cluster(id3) bootcluster(id3) weight(webb) level(95) seed(1) nograph 
return list 
insert_into_file using "sample_table.csv", key(P4) value(`r(p)') format(%5.2f)
sum Pecuniary if e_population == 1 & derp <= 4, det 
insert_into_file using "sample_table.csv", key(M4) value(`r(mean)') format(%5.2f)

ivreghdfe Pecuniary (lnpopulation = Z) i.id3 i.year  if e_population == 1 & derp > 4, cluster(id3) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es5) all
insert_into_file using "sample_table.csv", key(KP5) value(`e(rkf)') format(%5.2f)
boottest lnpopulation, cluster(id3) bootcluster(id3) weight(webb) level(95) seed(1) nograph 
return list 
insert_into_file using "sample_table.csv", key(P5) value(`r(p)') format(%5.2f)
sum Pecuniary if e_population == 1 & derp > 4, det 
insert_into_file using "sample_table.csv", key(M5) value(`r(mean)') format(%5.2f)

ivreghdfe Q52 (lnpopulation = Z) i.id3 i.year , cluster(id3) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es6) all
insert_into_file using "sample_table.csv", key(KP6) value(`e(rkf)') format(%5.2f)
boottest lnpopulation, cluster(id3) bootcluster(id3) weight(webb) level(95) seed(1) nograph 
return list 
insert_into_file using "sample_table.csv", key(P6) value(`r(p)') format(%5.2f)
sum Q52, det 
insert_into_file using "sample_table.csv", key(M6) value(`r(mean)') format(%5.2f)

ivreghdfe Q52 (lnpopulation = Z) i.id3 i.year if e_population == 1 & derp <= 4, cluster(id3)   
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es7) all
insert_into_file using "sample_table.csv", key(KP7) value(`e(rkf)') format(%5.2f)
boottest lnpopulation, cluster(id3) bootcluster(id3) weight(webb) level(95) seed(1) nograph 
return list 
insert_into_file using "sample_table.csv", key(P7) value(`r(p)') format(%5.2f)
sum Q52 if e_population == 1 & derp <= 4, det 
insert_into_file using "sample_table.csv", key(M7) value(`r(mean)') format(%5.2f)

ivreghdfe Q52 (lnpopulation = Z) i.id3 i.year  if e_population == 1 & derp > 4, cluster(id3) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es8) all
insert_into_file using "sample_table.csv", key(KP8) value(`e(rkf)') format(%5.2f)
boottest lnpopulation, cluster(id3) bootcluster(id3) weight(webb) level(95) seed(1) nograph 
return list 
insert_into_file using "sample_table.csv", key(P8) value(`r(p)') format(%5.2f)
sum Q52 if e_population == 1 & derp > 4, det 
insert_into_file using "sample_table.csv", key(M8) value(`r(mean)') format(%5.2f)

table_from_tpl, t(TPL_VCS2.tex) r(sample_table.csv) o(T_VCS_Rob.tex)


* Table A1: Urban population, OLS Estimates
* -----------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
estimates clear 

reghdfe lnpopcensus lnpopulation , cluster(id3) noabs
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es1) all

reghdfe lnpopcensus lnpopulation , cluster(id3) abs(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es2) all

table_from_tpl, t(TPL_POP1.tex) r(sample_table.csv) o(T_POP1.tex)


* Table A3: Descriptive statistics 
* --------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"

gen Burglary_pc = (crimetot8+ crimetot9) / population *100000
gen Robbery_pc = crimetot13 / population *100000
gen Robbery_ag_pc = crimetot31 / population *100000
gen Shoplifting_pc = crimetot36 / population *100000
gen Vehicle_pc = (crimetot39+ crimetot40) / population *100000
gen Pecuniary_pc =  (crimetot8+ crimetot9+crimetot13+crimetot31+crimetot36+crimetot39+crimetot40) / population *100000
gen Assault_pc = crimetot12 / population *100000
gen Assault_ag_pc = crimetot4 / population *100000
gen Murder_pc = (crimetot24+crimetot5) / population  *100000
gen NonPecpc = (crimetot12+crimetot4+crimetot24+crimetot5) / population *100000
replace population = population / 1000
foreach var of varlist population Pecuniary_pc Burglary_pc Robbery_pc Robbery_ag_pc Shoplifting_pc Vehicle_pc  NonPecpc Assault_pc Assault_ag_pc Murder_pc  {
	sum `var', det
	insert_into_file using "sample_table.csv", key(M`var') value(`r(mean)') format(%5.2f)
	sum `var', det
	insert_into_file using "sample_table.csv", key(SD`var') value(`r(sd)') format(%5.2f)
	sum `var', det
	pause 1000
	insert_into_file using "sample_table.csv", key(P10`var') value(`r(p10)') format(%5.2f)
	sum `var', det
	insert_into_file using "sample_table.csv", key(P90`var') value(`r(p90)') format(%5.2f)
}

table_from_tpl, t(TPL_DesStat.tex) r(sample_table.csv) o(T_DesStat.tex)


* Figure A1: Pecuniary and Non-pecuniary crime rates over the 2011-2018 period.
* ----------------------------------------------------------------------------
use "${directory}/ES_db.dta", clear  

gen Pecuniary_pc =  (crimetot8+ crimetot9+crimetot13+crimetot31+crimetot36+crimetot39+crimetot40) / population *100000
gen NonPecpc = (crimetot12+crimetot4+crimetot24+crimetot5) / population *100000
collapse (mean) Pecuniary_pc  NonPecpc, by(year )
twoway (line Pecuniary_pc year, legend(label(1 "Pecuniary crimes")) lpattern(longdash) yaxis(1) ytitle("Pecuniary crime rate")) (line NonPecpc year, lpattern(shortdash) yaxis(2) ytitle("Non-pecuniary crime rate",axis(2)) legend(label(2 "Non-pecuniary crimes")) graphregion(color(white)) bgcolor(white)) 
graph export "${figures}/F_crime.jpg", as(jpg) name("Graph") quality(100) replace 


* Figure A2: Annual urban population growth rate (in percent).
* ------------------------------------------------------------
use "${directory}/ES_db.dta", clear  

xtset id3 year
gen delta = (population / l.population - 1)*100
hist delta, graphregion(color(white)) bgcolor(white) xtitle("") ytitle("") //color(navy) 
graph export "${figures}/F_urbangrowth.eps", as(eps) name("Graph") preview(on) replace 


* Table B1: Emigration rate (2009-2011), OLS Estimates.
* -----------------------------------------------------
use "${directory}/ES_TableB1.dta", clear 
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

reghdfe amig_t Z, abs(id year) cluster(O_district)
store_est_tpl using "sample_table.csv", coef(Z) name(es1) all

reghdfe amig_u Z, abs(id year) cluster(O_district)
store_est_tpl using "sample_table.csv", coef(Z) name(es2) all

reghdfe amig_r Z, abs(id year) cluster(O_district)
store_est_tpl using "sample_table.csv", coef(Z) name(es3) all

table_from_tpl, t(TPL_Mig.tex) r(sample_table.csv) o(T_Mig.tex)


* Table B2: First stage, OLS Estimates
* ------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

reghdfe lnpopulation stdIV, cluster(id3) abs(id3) 
store_est_tpl using "sample_table.csv", coef(stdIV) name(es1) all

reghdfe lnpopulation stdIV, cluster(id3) abs(id3 year) 
store_est_tpl using "sample_table.csv", coef(stdIV) name(es2) all

table_from_tpl, t(TPL_FS.tex) r(sample_table.csv) o(T_FS.tex)


* Table B3: Pre-determined variables, OLS Estimates
* --------------------------------------------------
use "${directory}/ES_TableB3.dta", clear 
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

ols_spatial_HAC Education1 Xtemp missing_share constant, timevar(year) panelvar(id_m) lat(y_centro) lon(x_centro) distcutoff(150) 
store_est_tpl using "sample_table.csv", coef(Xtemp) name(es1) all
sum Education1 
insert_into_file using "sample_table.csv", key(mean1) value(`r(mean)') format(%5.2f)

ols_spatial_HAC EthnicGrp1 Xtemp missing_share constant, timevar(year) panelvar(id_m) lat(y_centro) lon(x_centro) distcutoff(150) 
store_est_tpl using "sample_table.csv", coef(Xtemp) name(es2) all
sum EthnicGrp1 
insert_into_file using "$sample_table.csv", key(mean2) value(`r(mean)') format(%5.2f)

ols_spatial_HAC EthnicGrp2 Xtemp missing_share constant, timevar(year) panelvar(id_m) lat(y_centro) lon(x_centro) distcutoff(150) 
store_est_tpl using "sample_table.csv", coef(Xtemp) name(es3) all
sum EthnicGrp2 
insert_into_file using "sample_table.csv", key(mean3) value(`r(mean)') format(%5.2f)

ols_spatial_HAC EthnicGrp3 Xtemp missing_share constant, timevar(year) panelvar(id_m) lat(y_centro) lon(x_centro) distcutoff(150) 
store_est_tpl using "sample_table.csv", coef(Xtemp) name(es4) all
sum EthnicGrp3 
insert_into_file using "sample_table.csv", key(mean4) value(`r(mean)') format(%5.2f)

ols_spatial_HAC EthnicGrp4 Xtemp missing_share constant, timevar(year) panelvar(id_m) lat(y_centro) lon(x_centro) distcutoff(150) 
store_est_tpl using "sample_table.csv", coef(Xtemp) name(es5) all
sum EthnicGrp4 
insert_into_file using "sample_table.csv", key(mean5) value(`r(mean)') format(%5.2f)

ols_spatial_HAC Employed Xtemp missing_share constant, timevar(year) panelvar(id_m) lat(y_centro) lon(x_centro) distcutoff(150) 
store_est_tpl using "sample_table.csv", coef(Xtemp) name(es6) all
sum Employed 
insert_into_file using "sample_table.csv", key(mean6) value(`r(mean)') format(%5.2f)

ols_spatial_HAC High_income Xtemp missing_share constant, timevar(year) panelvar(id_m) lat(y_centro) lon(x_centro) distcutoff(150) 
store_est_tpl using "sample_table.csv", coef(Xtemp) name(es7) all
sum High_income 
insert_into_file using "sample_table.csv", key(mean7) value(`r(mean)') format(%5.2f)

table_from_tpl, t(TPL_Predetermined.tex) r(sample_table.csv) o(T_Predetermined.tex)


* Table B4: Parallel trend assumption, OLS Estimates
* --------------------------------------------------
use "${directory}/ES_TableB4.dta", clear
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

ols_spatial_HAC lnpecuniary_V1 Xtemp missing_share constant, timevar(year) panelvar(id_m) lat(y_centro) lon(x_centro) distcutoff(150) 
store_est_tpl using "sample_table.csv", coef(Xtemp) name(es1) all
sum lnpecuniary_V1 
insert_into_file using "sample_table.csv", key(mean1) value(`r(mean)') format(%5.2f)

ols_spatial_HAC lnnonpecuniary Xtemp missing_share constant, timevar(year) panelvar(id_m) lat(y_centro) lon(x_centro) distcutoff(150) 
store_est_tpl using "sample_table.csv", coef(Xtemp) name(es2) all
sum lnnonpecuniary 
insert_into_file using "sample_table.csv", key(mean2) value(`r(mean)') format(%5.2f)

table_from_tpl, t(TPL_Parallel.tex) r(sample_table.csv) o(T_Parallel.tex)


* Table B5: Herfindahl index of origin contributions
* --------------------------------------------------
use "${directory}/ES_db.dta", clear  
keep if year == 2011
preserve 
use "${directory}/ES_shares.dta", clear 
drop if year != 2011
replace province = "Eastern Cape" if province == "Eastern cape"
replace province = "Free State" if province == "Free state"
replace province = "KwaZulu-Natal" if province == "Kwazulu-Natal"
replace province = "North West" if province == "North west"
replace province = "Northern Cape" if province == "Northern cape"
replace province = "Western Cape" if province == "Western cape"
replace local_municipality = "Emalahleni" if local_municipality == "Emalahleni-EC"
replace local_municipality = "Emalahleni" if local_municipality == "Emalahleni-MP"
replace local_municipality = "Ethekwini" if local_municipality == "eThekwini"
replace local_municipality = "Ga-Segonyana" if local_municipality == "Ga-Segonyane"
replace local_municipality = "Naledi" if local_municipality == "Naledi-FS"
replace local_municipality = "Naledi" if local_municipality == "Naledi-NW"
replace local_municipality = "UPhongolo" if local_municipality == "Uphongolo"
tempfile temp 
save `temp'
restore 
merge 1:m province local_municipality year using `temp'
drop if _merge != 3 
drop _merge 

bysort province local_municipality: egen mig_d = sum(migrants)
bysort province local_municipality: gen share_d = migrants/mig_d
bysort province local_municipality: gen share_d2 = share_d^2
bysort province local_municipality: egen share_dmax = max(share_d)

preserve 
collapse (sum) share_d2 (mean) share_dmax, by(province local_municipality year)

cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

foreach var of varlist share_d2 share_dmax {
	sum `var', det
	insert_into_file using "sample_table.csv", key(M`var') value(`r(mean)') format(%5.2f)
	sum `var', det
	insert_into_file using "sample_table.csv", key(SD`var') value(`r(sd)') format(%5.2f)
	sum `var', det
	pause 1000
	insert_into_file using "sample_table.csv", key(P10`var') value(`r(p10)') format(%5.2f)
	sum `var', det
	insert_into_file using "sample_table.csv", key(P90`var') value(`r(p90)') format(%5.2f)
}
restore 

keep if year == 2011
preserve 
use "${directory}/ES_shares.dta", clear 
drop if year != 2011
replace province = "Eastern Cape" if province == "Eastern cape"
replace province = "Free State" if province == "Free state"
replace province = "KwaZulu-Natal" if province == "Kwazulu-Natal"
replace province = "North West" if province == "North west"
replace province = "Northern Cape" if province == "Northern cape"
replace province = "Western Cape" if province == "Western cape"
replace local_municipality = "Emalahleni" if local_municipality == "Emalahleni-EC"
replace local_municipality = "Emalahleni" if local_municipality == "Emalahleni-MP"
replace local_municipality = "Ethekwini" if local_municipality == "eThekwini"
replace local_municipality = "Ga-Segonyana" if local_municipality == "Ga-Segonyane"
replace local_municipality = "Naledi" if local_municipality == "Naledi-FS"
replace local_municipality = "Naledi" if local_municipality == "Naledi-NW"
replace local_municipality = "UPhongolo" if local_municipality == "Uphongolo"

gen O_district = . 
replace O_district = 101 if P11C_PREVRESMUNIC >= 160 & P11C_PREVRESMUNIC <= 164  
replace O_district = 102 if P11C_PREVRESMUNIC >= 165 & P11C_PREVRESMUNIC <= 169
replace O_district = 103 if P11C_PREVRESMUNIC >= 170 & P11C_PREVRESMUNIC <= 173  
replace O_district = 104 if P11C_PREVRESMUNIC >= 174 & P11C_PREVRESMUNIC <= 180
replace O_district = 105 if P11C_PREVRESMUNIC >= 181  & P11C_PREVRESMUNIC <= 183
replace O_district = 199 if P11C_PREVRESMUNIC >= 199 & P11C_PREVRESMUNIC <= 199  
replace O_district = 210 if P11C_PREVRESMUNIC >= 261 & P11C_PREVRESMUNIC <= 269
replace O_district = 212 if (P11C_PREVRESMUNIC >= 270 & P11C_PREVRESMUNIC <= 274) | P11C_PREVRESMUNIC == 276 | P11C_PREVRESMUNIC == 277
replace O_district = 213 if P11C_PREVRESMUNIC >= 278 & P11C_PREVRESMUNIC <= 285
replace O_district = 214 if P11C_PREVRESMUNIC >= 286 & P11C_PREVRESMUNIC <= 289
replace O_district = 215 if P11C_PREVRESMUNIC >= 290 & P11C_PREVRESMUNIC <= 294
replace O_district = 244 if P11C_PREVRESMUNIC >= 295 & P11C_PREVRESMUNIC <= 298  
replace O_district = 260 if P11C_PREVRESMUNIC >= 260 & P11C_PREVRESMUNIC <= 260  
replace O_district = 299 if P11C_PREVRESMUNIC >= 299 & P11C_PREVRESMUNIC <= 299  
replace O_district = 306 if P11C_PREVRESMUNIC >= 363  & P11C_PREVRESMUNIC <= 368 
replace O_district = 307 if P11C_PREVRESMUNIC >= 369 & P11C_PREVRESMUNIC <= 376  
replace O_district = 308 if P11C_PREVRESMUNIC >= 377 & P11C_PREVRESMUNIC <= 382 
replace O_district = 309 if P11C_PREVRESMUNIC >= 383 & P11C_PREVRESMUNIC <= 386  
replace O_district = 345 if P11C_PREVRESMUNIC >= 360 & P11C_PREVRESMUNIC <= 362   
replace O_district = 416 if P11C_PREVRESMUNIC >= 460 & P11C_PREVRESMUNIC <= 463  
replace O_district = 418 if P11C_PREVRESMUNIC >= 464 & P11C_PREVRESMUNIC <= 468  
replace O_district = 419 if P11C_PREVRESMUNIC >= 469 & P11C_PREVRESMUNIC <= 474  
replace O_district = 420 if P11C_PREVRESMUNIC == 475 | (P11C_PREVRESMUNIC >= 477 & P11C_PREVRESMUNIC <= 479)   
replace O_district = 499 if P11C_PREVRESMUNIC >= 499 & P11C_PREVRESMUNIC <= 499 
replace O_district = 521 if (P11C_PREVRESMUNIC >= 503 & P11C_PREVRESMUNIC <= 506) | P11C_PREVRESMUNIC == 560 | P11C_PREVRESMUNIC == 561
replace O_district = 522 if P11C_PREVRESMUNIC >= 562 & P11C_PREVRESMUNIC <= 568  
replace O_district = 523 if P11C_PREVRESMUNIC == 514 | P11C_PREVRESMUNIC == 569  | P11C_PREVRESMUNIC == 570 | P11C_PREVRESMUNIC == 571 | P11C_PREVRESMUNIC == 573
replace O_district = 527 if P11C_PREVRESMUNIC >= 582 & P11C_PREVRESMUNIC <= 586  
replace O_district = 528 if P11C_PREVRESMUNIC == 538 | P11C_PREVRESMUNIC == 542 | (P11C_PREVRESMUNIC >= 587 & P11C_PREVRESMUNIC <= 590)   
replace O_district = 543 if P11C_PREVRESMUNIC >= 594 & P11C_PREVRESMUNIC <= 598  
replace O_district = 554 if P11C_PREVRESMUNIC >= 574 & P11C_PREVRESMUNIC <= 577
replace O_district = 555 if P11C_PREVRESMUNIC >= 524 & P11C_PREVRESMUNIC <= 526  
replace O_district = 556 if (P11C_PREVRESMUNIC >= 578  & P11C_PREVRESMUNIC <= 581) |   P11C_PREVRESMUNIC == 529
replace O_district = 559 if P11C_PREVRESMUNIC == 546 | P11C_PREVRESMUNIC == 591 | P11C_PREVRESMUNIC == 592 | P11C_PREVRESMUNIC == 593   
replace O_district = 599 if P11C_PREVRESMUNIC >= 599 & P11C_PREVRESMUNIC <= 599  
replace O_district = 637 if P11C_PREVRESMUNIC >= 660 & P11C_PREVRESMUNIC <= 664  
replace O_district = 638 if P11C_PREVRESMUNIC >= 665 & P11C_PREVRESMUNIC <= 669  
replace O_district = 639 if P11C_PREVRESMUNIC >= 670 & P11C_PREVRESMUNIC <= 674  
replace O_district = 640 if P11C_PREVRESMUNIC >= 675 & P11C_PREVRESMUNIC <= 678  
replace O_district = 742 if P11C_PREVRESMUNIC >= 760 & P11C_PREVRESMUNIC <= 762  
replace O_district = 748 if P11C_PREVRESMUNIC >= 763  & P11C_PREVRESMUNIC <= 766  
replace O_district = 797 if P11C_PREVRESMUNIC >= 797 & P11C_PREVRESMUNIC <= 797
replace O_district = 798 if P11C_PREVRESMUNIC >= 798 & P11C_PREVRESMUNIC <=  798 
replace O_district = 799 if P11C_PREVRESMUNIC >= 799  & P11C_PREVRESMUNIC <= 799   
replace O_district = 830 if P11C_PREVRESMUNIC >= 860 & P11C_PREVRESMUNIC <= 866 
replace O_district = 831 if P11C_PREVRESMUNIC >= 867 & P11C_PREVRESMUNIC <= 872 
replace O_district = 832 if P11C_PREVRESMUNIC >= 873 & P11C_PREVRESMUNIC <= 877 
replace O_district = 933 if P11C_PREVRESMUNIC >= 960 & P11C_PREVRESMUNIC <= 964 
replace O_district = 934 if P11C_PREVRESMUNIC >= 965 & P11C_PREVRESMUNIC <= 968 
replace O_district = 935 if P11C_PREVRESMUNIC >= 969 & P11C_PREVRESMUNIC <= 976 
replace O_district = 936 if P11C_PREVRESMUNIC >= 977 & P11C_PREVRESMUNIC <= 982 
replace O_district = 947 if P11C_PREVRESMUNIC >= 983 & P11C_PREVRESMUNIC <= 987

collapse (sum) migrants , by(province local_municipality O_district year)
bysort province local_municipality: egen mig_d = sum(migrants)
bysort province local_municipality: gen share_dd = migrants/mig_d
bysort province local_municipality: gen share_dd2 = share_d^2
collapse (sum) share_dd2 , by(province local_municipality year)
tempfile temp 
save `temp'
restore 
merge 1:m province local_municipality year using `temp'
drop if _merge != 3 
drop _merge 

cd "${tables}"
estimates clear 
foreach var of varlist share_dd2 {
	sum `var', det
	insert_into_file using "sample_table.csv", key(M`var') value(`r(mean)') format(%5.2f)
	sum `var', det
	insert_into_file using "sample_table.csv", key(SD`var') value(`r(sd)') format(%5.2f)
	sum `var', det
	pause 1000
	insert_into_file using "sample_table.csv", key(P10`var') value(`r(p10)') format(%5.2f)
	sum `var', det
	insert_into_file using "sample_table.csv", key(P90`var') value(`r(p90)') format(%5.2f)
}

table_from_tpl, t(TPL_DesStat_HI.tex) r(sample_table.csv) o(T_DesStat_HI.tex)


* Table B6: Intra Class Correlation
* ---------------------------------
use "${directory}/ES_TableB6.dta", clear
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

mixed Xtemp i.year || O_province: || id3: || , vce(robust)
estat icc
return list 
insert_into_file using "sample_table.csv", key(icc_3) value(`r(icc3)') format(%5.2f)
estat icc
insert_into_file using "sample_table.csv", key(se_3) value(`r(se3)') format(%5.2f)
estat icc
insert_into_file using "sample_table.csv", key(icc_2) value(`r(icc2)') format(%5.2f)
estat icc
insert_into_file using "sample_table.csv", key(se_2) value(`r(se2)') format(%5.2f)

table_from_tpl, t(TPL_ICC.tex) r(sample_table.csv) o(T_ICC.tex)


* Table B7: Origin based specification, IV Estimates
* ---------------------------------------------------
use "${directory}/ES_TableB7.dta", clear
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

ivreghdfe lnpecuniary_V1 ( lnpopulation = Xtemp), abs( O_municipality) cluster( O_district )
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es1) all
insert_into_file using "sample_table.csv", key(KP1) value(`e(rkf)') format(%5.2f)

ivreghdfe lnnonpecuniary ( lnpopulation = Xtemp), abs( O_municipality) cluster( O_district )
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es2) all
insert_into_file using "sample_table.csv", key(KP2) value(`e(rkf)') format(%5.2f)

table_from_tpl, t(TPL_IV1.tex) r(sample_table.csv) o(T_IV_equi.tex)


* Table B8: Lagged shock, OLS Estimates
* -------------------------------------
use "${directory}/ES_TableB8.dta", clear
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

ols_spatial_HAC Pecuniary Xtemp missing_share constant, timevar(year) panelvar(id_m) lat(y_centro) lon(x_centro) distcutoff(150) 
store_est_tpl using "sample_table.csv", coef(Xtemp) name(es1) all
sum Pecuniary 
insert_into_file using "sample_table.csv", key(mean1) value(`r(mean)') format(%5.2f)

ols_spatial_HAC NonPecuniary Xtemp missing_share constant, timevar(year) panelvar(id_m) lat(y_centro) lon(x_centro) distcutoff(150) 
store_est_tpl using "sample_table.csv", coef(Xtemp) name(es2) all
sum NonPecuniary 
insert_into_file using "sample_table.csv", key(mean2) value(`r(mean)') format(%5.2f)

table_from_tpl, t(TPL_Laggedshock.tex) r(sample_table.csv) o(T_Laggedshock.tex)


* Table C1: Sub-sample by initial population size, IV Estimates
* -------------------------------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

ivreghdfe lnpecuniary_V1 (lnpopulation = Z) if Population_Census < 48000, cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es1) all
insert_into_file using "sample_table.csv", key(KP1) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people (lnpopulation = Z) if Population_Census < 48000, cluster(id3) abs(id3 year) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es2) all
insert_into_file using "sample_table.csv", key(KP2) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1 (lnpopulation = Z) if Population_Census >= 48000, cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es3) all
insert_into_file using "sample_table.csv", key(KP3) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people (lnpopulation = Z) if Population_Census >= 48000, cluster(id3) abs(id3 year) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es4) all
insert_into_file using "sample_table.csv", key(KP4) value(`e(rkf)') format(%5.2f)

table_from_tpl, t(TPL_SPLIT.tex) r(sample_table.csv) o(T_SPLIT.tex)


* Table C2: Crime categories, IV Estimates
* ----------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

gen burglary = crimetot8 + crimetot9
gen robbery = crimetot13
gen agg_robbery = crimetot31
gen shoplifting = crimetot36
gen vehicle = crimetot39 + crimetot40
gen assault = crimetot12
gen agg_assault = crimetot4
gen murder = crimetot5 + crimetot24

foreach var of varlist crimetot* burglary robbery agg_robbery shoplifting vehicle assault agg_assault murder {
	gen ln`var'pc = asinh(`var'/population*100000)
}

ivreghdfe lnburglarypc (lnpopulation = IV), abs(id3 year) cluster(id3) // Burglary 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es1) all
insert_into_file using "sample_table.csv", key(KP1) value(`e(rkf)') format(%5.2f)

ivreghdfe lnrobberypc (lnpopulation = IV) , abs(id3 year) cluster(id3) // Robbery
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es2) all
insert_into_file using "sample_table.csv", key(KP2) value(`e(rkf)') format(%5.2f)

ivreghdfe lnagg_robberypc (lnpopulation = IV), abs(id3 year) cluster(id3) // Robbery with aggravated circumstances 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es3) all
insert_into_file using "sample_table.csv", key(KP3) value(`e(rkf)') format(%5.2f)

ivreghdfe lnshopliftingpc (lnpopulation = IV), abs(id3 year) cluster(id3) // Shoplifting
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es4) all
insert_into_file using "sample_table.csv", key(KP4) value(`e(rkf)') format(%5.2f)

ivreghdfe lnvehiclepc (lnpopulation = IV), abs(id3 year) cluster(id3) // Theft out or of motor vehicle
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es5) all
insert_into_file using "sample_table.csv", key(KP5) value(`e(rkf)') format(%5.2f)

ivreghdfe lncrimetot12pc (lnpopulation = IV), abs(id3 year) cluster(id3) // Common assault 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es6) all
insert_into_file using "sample_table.csv", key(KP6) value(`e(rkf)') format(%5.2f)

ivreghdfe lncrimetot4pc (lnpopulation = IV), abs(id3 year) cluster(id3) // Assault with intent to inflict 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es7) all
insert_into_file using "sample_table.csv", key(KP7) value(`e(rkf)') format(%5.2f)

ivreghdfe lnmurderpc (lnpopulation = IV), abs(id3 year) cluster(id3) // (Attempted) murder 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es8) all
insert_into_file using "sample_table.csv", key(KP8) value(`e(rkf)') format(%5.2f)

table_from_tpl, t(TPL_Dis2.tex) r(sample_table.csv) o(T_Dis2.tex)


* Figure D1: Effect of education on victimization, OLS Estimates
* --------------------------------------------------------------
use "${directory}/ES_FigureD1.dta", clear
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

foreach var of varlist CrimeExperience__1 pec {
reghdfe `var' i.Educ [pweight = pers_pstrwgt], abs(P_MUNIC) cluster(P_MUNIC)
coefplot, yline(0) drop(_cons) vert graphregion(color(white)) bgcolor(white) xlabel(, ang(45))
graph export "${Figures}/`var'.eps", as(eps) name("Graph") preview(off) replace
}


* Table E1: Pecuniary crimes, Augmented shift-share, IV Estimates
* ---------------------------------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d0_e0) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es1) all
insert_into_file using "sample_table.csv", key(KP1) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d5_e0) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es2) all
insert_into_file using "sample_table.csv", key(KP2) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d15_e0) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es3) all
insert_into_file using "sample_table.csv", key(KP3) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d20_e0) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es4) all
insert_into_file using "sample_table.csv", key(KP4) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d0_e5) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es5) all
insert_into_file using "sample_table.csv", key(KP5) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d5_e5) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es6) all
insert_into_file using "sample_table.csv", key(KP6) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d15_e5) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es7) all
insert_into_file using "sample_table.csv", key(KP7) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d20_e5) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es8) all
insert_into_file using "sample_table.csv", key(KP8) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d0_e15) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es9) all
insert_into_file using "sample_table.csv", key(KP9) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d5_e15) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es10) all
insert_into_file using "sample_table.csv", key(KP10) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d15_e15) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es11) all
insert_into_file using "sample_table.csv", key(KP11) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d20_e15) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es12) all
insert_into_file using "sample_table.csv", key(KP12) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d0_e20) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es13) all
insert_into_file using "sample_table.csv", key(KP13) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d5_e20) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es14) all
insert_into_file using "sample_table.csv", key(KP14) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d15_e20) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es15) all
insert_into_file using "sample_table.csv", key(KP15) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV_d20_e20) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es16) all
insert_into_file using "sample_table.csv", key(KP16) value(`e(rkf)') format(%5.2f)

table_from_tpl, t(TPL_GreenbergReg.tex) r(sample_table.csv) o(T_GreenbergReg.tex)


* Table E2: Non-pecuniary crimes, Augmented shift-share, IV Estimates
* --------------------------------------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d0_e0) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es1) all
insert_into_file using "sample_table.csv", key(KP1) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d5_e0) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es2) all
insert_into_file using "sample_table.csv", key(KP2) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d15_e0) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es3) all
insert_into_file using "sample_table.csv", key(KP3) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d20_e0) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es4) all
insert_into_file using "sample_table.csv", key(KP4) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d0_e5) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es5) all
insert_into_file using "sample_table.csv", key(KP5) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d5_e5) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es6) all
insert_into_file using "sample_table.csv", key(KP6) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d15_e5) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es7) all
insert_into_file using "sample_table.csv", key(KP7) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d20_e5) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es8) all
insert_into_file using "sample_table.csv", key(KP8) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d0_e15) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es9) all
insert_into_file using "sample_table.csv", key(KP9) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d5_e15) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es10) all
insert_into_file using "sample_table.csv", key(KP10) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d15_e15) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es11) all
insert_into_file using "sample_table.csv", key(KP11) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d20_e15) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es12) all
insert_into_file using "sample_table.csv", key(KP12) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d0_e20) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es13) all
insert_into_file using "sample_table.csv", key(KP13) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d5_e20) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es14) all
insert_into_file using "sample_table.csv", key(KP14) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d15_e20) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es15) all
insert_into_file using "sample_table.csv", key(KP15) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV_d20_e20) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es16) all
insert_into_file using "sample_table.csv", key(KP16) value(`e(rkf)') format(%5.2f)

table_from_tpl, t(TPL_GreenbergReg.tex) r(sample_table.csv) o(T_GreenbergReg_NP.tex)


* Figure F1: Marginal effects for pecuniary crimes, OLS Estimates
* ---------------------------------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

reghdfe lnpecuniary_V1 c.lnpopulation##c.Herfindahl , abs(id3 year) cluster(id3) 
margins, dydx(lnpopulation) at(Herfindahl=(0.05(0.05)0.6)) noestimcheck
marginsplot, addplot(hist Herfindahl, percent yaxis(2) yscale(alt axis(2)) xtitle(Herfindahl index)) yline(0) legend(off) graphregion(color(white)) ytitle("Marginal effect of ln(population)") title("")
graph export "${Figures}/F_Social_ME_Pec.eps", as(eps) name("Graph") preview(on)

* Figure F2: Marginal effects for non-pecuniary crimes, OLS Estimates
* -------------------------------------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear 

reghdfe lnNonpecu_people c.lnpopulation##c.Herfindahl , abs(id3 year) cluster(id3) 
margins, dydx(lnpopulation) at(Herfindahl=(0.05(0.05)0.6)) noestimcheck
marginsplot, addplot(hist Herfindahl, percent yaxis(2) yscale(alt axis(2)) xtitle(Herfindahl index)) yline(0) legend(off) graphregion(color(white)) ytitle("Marginal effect of ln(population)") title("")
graph export "${Figures}/F_Social_ME_NPec.eps", as(eps) name("Graph") preview(on)


* Table G1: Potential transmission channels, IV Estimates
* -------------------------------------------------------
use "${directory}/ES_TableG1_nids.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear

ivreghdfe Work (lnpopulation = IV) [pweight = weight], abs(pid year) cluster(district)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esW1) all
insert_into_file using "sample_table.csv", key(KPW1) value(`e(rkf)') format(%5.2f)

ivreghdfe employed_nat (lnpopulation = IV) [pweight = weight], abs(pid year) cluster(district)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esW2) all
insert_into_file using "sample_table.csv", key(KPW2) value(`e(rkf)') format(%5.2f)

ivreghdfe employed_mig (lnpopulation = IV) [pweight = weight], abs(pid year) cluster(district)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esW3) all
insert_into_file using "sample_table.csv", key(KPW3) value(`e(rkf)') format(%5.2f)

ivreghdfe employed_LS (lnpopulation = IV) [pweight = weight], abs(pid year) cluster(district)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esW4) all
insert_into_file using "sample_table.csv", key(KPW4) value(`e(rkf)') format(%5.2f)

ivreghdfe employed_MS (lnpopulation = IV) [pweight = weight], abs(pid year) cluster(district)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esW5) all
insert_into_file using "sample_table.csv", key(KPW5) value(`e(rkf)') format(%5.2f)

ivreghdfe employed_HS (lnpopulation = IV) [pweight = weight], abs(pid year) cluster(district)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esW6) all
insert_into_file using "sample_table.csv", key(KPW6) value(`e(rkf)') format(%5.2f)

ivreghdfe lnincome (lnpopulation = IV) [pweight = weight], abs(pid year) cluster(district)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esW7) all
insert_into_file using "sample_table.csv", key(KPW7) value(`e(rkf)') format(%5.2f)

ivreghdfe lnincome_nat (lnpopulation = IV) [pweight = weight], abs(pid year) cluster(district)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esW8) all
insert_into_file using "sample_table.csv", key(KPW8) value(`e(rkf)') format(%5.2f)

ivreghdfe lnincome_mig (lnpopulation = IV) [pweight = weight], abs(pid year) cluster(district)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esW9) all
insert_into_file using "sample_table.csv", key(KPW9) value(`e(rkf)') format(%5.2f)

ivreghdfe lnincome_LS (lnpopulation = IV) [pweight = weight], abs(pid year) cluster(district)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esW10) all
insert_into_file using "sample_table.csv", key(KPW10) value(`e(rkf)') format(%5.2f)

ivreghdfe lnincome_MS (lnpopulation = IV) [pweight = weight], abs(pid year) cluster(district)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esW11) all
insert_into_file using "sample_table.csv", key(KPW11) value(`e(rkf)') format(%5.2f)

ivreghdfe lnincome_HS (lnpopulation = IV) [pweight = weight], abs(pid year) cluster(district)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esW12) all
insert_into_file using "sample_table.csv", key(KPW12) value(`e(rkf)') format(%5.2f)

sum Work 
insert_into_file using "sample_table.csv", key(meanW1) value(`r(mean)') format(%5.2f)
sum employed_nat
insert_into_file using "sample_table.csv", key(meanW2) value(`r(mean)') format(%5.2f)
sum employed_mig 
insert_into_file using "sample_table.csv", key(meanW3) value(`r(mean)') format(%5.2f)
sum employed_LS
insert_into_file using "sample_table.csv", key(meanW4) value(`r(mean)') format(%5.2f)
sum employed_MS
insert_into_file using "sample_table.csv", key(meanW5) value(`r(mean)') format(%5.2f)
sum employed_HS
insert_into_file using "sample_table.csv", key(meanW6) value(`r(mean)') format(%5.2f)
sum lnincome 
insert_into_file using "sample_table.csv", key(meanW7) value(`r(mean)') format(%5.2f)
sum lnincome_nat
insert_into_file using "sample_table.csv", key(meanW8) value(`r(mean)') format(%5.2f)
sum lnincome_mig 
insert_into_file using "sample_table.csv", key(meanW9) value(`r(mean)') format(%5.2f)
sum lnincome_LS
insert_into_file using "sample_table.csv", key(meanW10) value(`r(mean)') format(%5.2f)
sum lnincome_MS
insert_into_file using "sample_table.csv", key(meanW11) value(`r(mean)') format(%5.2f)
sum lnincome_HS
insert_into_file using "sample_table.csv", key(meanW12) value(`r(mean)') format(%5.2f)

use "${directory}/ES_db.dta", clear  
capture gen Education12 = Education1+ Education2
ivreghdfe AgeGroup1 (lnpopulation = Z) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esD1) all
insert_into_file using "sample_table.csv", key(KPD1) value(`e(rkf)') format(%5.2f)

ivreghdfe AgeGroup2 (lnpopulation = Z) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esD2) all
insert_into_file using "sample_table.csv", key(KPD2) value(`e(rkf)') format(%5.2f)

ivreghdfe AgeGroup3 (lnpopulation = Z) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esD3) all
insert_into_file using "sample_table.csv", key(KPD3) value(`e(rkf)') format(%5.2f)

ivreghdfe AgeGroup4 (lnpopulation = Z) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esD4) all
insert_into_file using "sample_table.csv", key(KPD4) value(`e(rkf)') format(%5.2f)

ivreghdfe Sex2 (lnpopulation = Z) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esD5) all
insert_into_file using "sample_table.csv", key(KPD5) value(`e(rkf)') format(%5.2f)

ivreghdfe Education12 (lnpopulation = Z) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esD6) all
insert_into_file using "sample_table.csv", key(KPD6) value(`e(rkf)') format(%5.2f)

sum AgeGroup1 
insert_into_file using "sample_table.csv", key(meanD1) value(`r(mean)') format(%5.2f)
sum AgeGroup2
insert_into_file using "sample_table.csv", key(meanD2) value(`r(mean)') format(%5.2f)
sum AgeGroup3 
insert_into_file using "sample_table.csv", key(meanD3) value(`r(mean)') format(%5.2f)
sum AgeGroup4
insert_into_file using "sample_table.csv", key(meanD4) value(`r(mean)') format(%5.2f)
sum Sex2
insert_into_file using "sample_table.csv", key(meanD5) value(`r(mean)') format(%5.2f)
sum Education12
insert_into_file using "sample_table.csv", key(meanD6) value(`r(mean)') format(%5.2f)

ivreghdfe EthnicGrp1 (lnpopulation = Z) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esE1) all
insert_into_file using "sample_table.csv", key(KPE1) value(`e(rkf)') format(%5.2f)

ivreghdfe EthnicGrp2 (lnpopulation = Z) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esE2) all
insert_into_file using "sample_table.csv", key(KPE2) value(`e(rkf)') format(%5.2f)

ivreghdfe EthnicGrp3 (lnpopulation = Z) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esE3) all
insert_into_file using "sample_table.csv", key(KPE3) value(`e(rkf)') format(%5.2f)

ivreghdfe EthnicGrp4 (lnpopulation = Z) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esE4) all
insert_into_file using "sample_table.csv", key(KPE4) value(`e(rkf)') format(%5.2f)

sum EthnicGrp1 
insert_into_file using "sample_table.csv", key(meanE1) value(`r(mean)') format(%5.2f)
sum EthnicGrp2
insert_into_file using "sample_table.csv", key(meanE2) value(`r(mean)') format(%5.2f)
sum EthnicGrp3 
insert_into_file using "sample_table.csv", key(meanE3) value(`r(mean)') format(%5.2f)
sum EthnicGrp4
insert_into_file using "sample_table.csv", key(meanE4) value(`r(mean)') format(%5.2f)

ivreghdfe NMigrants (lnpopulation = Z) , cluster(id3) abs(id3 year)  
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esS1) all
insert_into_file using "sample_table.csv", key(KPS1) value(`e(rkf)') format(%5.2f)

ivreghdfe NMigrants_SC (lnpopulation = Z), cluster(id3) abs(id3 year) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esS2) all
insert_into_file using "sample_table.csv", key(KPS2) value(`e(rkf)') format(%5.2f)

ivreghdfe NMigrants_OC (lnpopulation = Z) , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esS3) all
insert_into_file using "sample_table.csv", key(KPS3) value(`e(rkf)') format(%5.2f)

sum NMigrants 
insert_into_file using "sample_table.csv", key(meanS1) value(`r(mean)') format(%5.2f)
sum NMigrants_SC
insert_into_file using "sample_table.csv", key(meanS2) value(`r(mean)') format(%5.2f)
sum NMigrants_OC 
insert_into_file using "sample_table.csv", key(meanS3) value(`r(mean)') format(%5.2f)

ivreghdfe BUR_ (lnpopulation = Z), abs(id3 year) cluster(id3)  
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esL1) all
insert_into_file using "sample_table.csv", key(KPL1) value(`e(rkf)') format(%5.2f)

ivreghdfe BUS_ (lnpopulation = Z), abs(id3 year) cluster(id3) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esL2) all
insert_into_file using "sample_table.csv", key(KPL2) value(`e(rkf)') format(%5.2f)

ivreghdfe BUC_ (lnpopulation = Z), abs(id3 year) cluster(id3) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esL3) all
insert_into_file using "sample_table.csv", key(KPL3) value(`e(rkf)') format(%5.2f)

ivreghdfe BUI_ (lnpopulation = Z), abs(id3 year) cluster(id3) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esL4) all
insert_into_file using "sample_table.csv", key(KPL4) value(`e(rkf)') format(%5.2f)

ivreghdfe S_Occup (lnpopulation = Z), abs(id3 year) cluster(id3) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esL5) all
insert_into_file using "sample_table.csv", key(KPL5) value(`e(rkf)') format(%5.2f)

sum BUR_ 
insert_into_file using "sample_table.csv", key(meanL1) value(`r(mean)') format(%5.2f)
sum BUS_
insert_into_file using "sample_table.csv", key(meanL2) value(`r(mean)') format(%5.2f)
sum BUC_ 
insert_into_file using "sample_table.csv", key(meanL3) value(`r(mean)') format(%5.2f)
sum BUI_
insert_into_file using "sample_table.csv", key(meanL4) value(`r(mean)') format(%5.2f)
sum S_Occup
insert_into_file using "sample_table.csv", key(meanL5) value(`r(mean)') format(%5.2f)

table_from_tpl, t(TPL_Mech.tex) r(sample_table.csv) o(T_Mech0.tex)


* Table G2: Public goods, OLS Estimates
* -------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear

gen lnPoliceper100000 = ln(Policeper100000)
gen pop16 = population if year == 2016
bysort id3 : egen population2016 = max(pop16) 
gen lnpopulation2016 = ln(population2016)
gen pop11 = population if year == 2011
bysort id3 : egen population2011 = max(pop11) 
gen lnpopulation2011 = ln(population2011)
gen Pop11_16 = population2016 - population2011 
gen lnPop11_16 = lnpopulation2016 - lnpopulation2011

reghdfe Policeper100000 lnPop11_16 lnpopulation2011 , cluster(id3) noabs
store_est_tpl using "sample_table.csv", coef(lnPop11_16) name(es1a) all
store_est_tpl using "sample_table.csv", coef(lnpopulation2011) name(es1b) all

reghdfe RateWater lnPop11_16 lnpopulation2011 , cluster(id3) noabs
store_est_tpl using "sample_table.csv", coef(lnPop11_16) name(es2a) all
store_est_tpl using "sample_table.csv", coef(lnpopulation2011) name(es2b) all

reghdfe RateRefuse lnPop11_16 lnpopulation2011 , cluster(id3) noabs
store_est_tpl using "sample_table.csv", coef(lnPop11_16) name(es3a) all
store_est_tpl using "sample_table.csv", coef(lnpopulation2011) name(es3b) all

reghdfe RateElectricity lnPop11_16 lnpopulation2011 , cluster(id3) noabs
store_est_tpl using "sample_table.csv", coef(lnPop11_16) name(es4a) all
store_est_tpl using "sample_table.csv", coef(lnpopulation2011) name(es4b) all

reghdfe RateToilet lnPop11_16 lnpopulation2011 , cluster(id3) noabs
store_est_tpl using "sample_table.csv", coef(lnPop11_16) name(es5a) all
store_est_tpl using "sample_table.csv", coef(lnpopulation2011) name(es5b) all

reghdfe RateHospital lnPop11_16 lnpopulation2011 , cluster(id3) noabs
store_est_tpl using "sample_table.csv", coef(lnPop11_16) name(es6a) all
store_est_tpl using "sample_table.csv", coef(lnpopulation2011) name(es6b) all

reghdfe RateClinic lnPop11_16 lnpopulation2011 , cluster(id3) noabs
store_est_tpl using "sample_table.csv", coef(lnPop11_16) name(es7a) all
store_est_tpl using "sample_table.csv", coef(lnpopulation2011) name(es7b) all

reghdfe RatePolice lnPop11_16 lnpopulation2011 , cluster(id3) noabs
store_est_tpl using "sample_table.csv", coef(lnPop11_16) name(es8a) all
store_est_tpl using "sample_table.csv", coef(lnpopulation2011) name(es8b) all

reghdfe RateSchool lnPop11_16 lnpopulation2011 , cluster(id3) noabs
store_est_tpl using "sample_table.csv", coef(lnPop11_16) name(es9a) all
store_est_tpl using "sample_table.csv", coef(lnpopulation2011) name(es9b) all

sum Policeper100000 
insert_into_file using "sample_table.csv", key(mean1) value(`r(mean)') format(%5.2f)
sum RateWater
insert_into_file using "sample_table.csv", key(mean2) value(`r(mean)') format(%5.2f)
sum RateRefuse 
insert_into_file using "sample_table.csv", key(mean3) value(`r(mean)') format(%5.2f)
sum RateElectricity
insert_into_file using "sample_table.csv", key(mean4) value(`r(mean)') format(%5.2f)
sum RateToilet
insert_into_file using "sample_table.csv", key(mean5) value(`r(mean)') format(%5.2f)
sum RateHospital 
insert_into_file using "sample_table.csv", key(mean6) value(`r(mean)') format(%5.2f)
sum RateClinic
insert_into_file using "sample_table.csv", key(mean7) value(`r(mean)') format(%5.2f)
sum RatePolice
insert_into_file using "sample_table.csv", key(mean8) value(`r(mean)') format(%5.2f)
sum RateSchool 
insert_into_file using "sample_table.csv", key(mean9) value(`r(mean)') format(%5.2f)

table_from_tpl, t(TPL_PublicG.tex) r(sample_table.csv) o(T_PubGood.tex)


* Table G3: Mediation analysis, IV Estimates
* ------------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear

gen Education12 = Education1+ Education2
gen lnEducation12 = ln(Education12)
gen lnBlack = asinh(EthnicGrp1)
gen lnAge2 = ln(AgeGroup2)

ivreghdfe lnpecuniary_V1 (lnpopulation = Z) if lnEducation12 != . , abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa1) all
insert_into_file using "sample_table.csv", key(KPP1) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1 (lnpopulation = Z) lnEducation12 , abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa2) all
store_est_tpl using "sample_table.csv", coef(lnEducation12) name(esPb2) all
insert_into_file using "sample_table.csv", key(KPP2) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1 (lnpopulation = Z) lnBlack, abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa3) all
store_est_tpl using "sample_table.csv", coef(lnBlack) name(esPc3) all
insert_into_file using "sample_table.csv", key(KPP3) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1 (lnpopulation = Z) lnAge2, abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa4) all
store_est_tpl using "sample_table.csv", coef(lnAge2) name(esPd4) all
insert_into_file using "sample_table.csv", key(KPP4) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1 (lnpopulation = Z) lnE12 lnBlack lnAge2, abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa5) all
store_est_tpl using "sample_table.csv", coef(lnE12) name(esPb5) all
store_est_tpl using "sample_table.csv", coef(lnBlack) name(esPc5) all
store_est_tpl using "sample_table.csv", coef(lnAge2) name(esPd5) all
insert_into_file using "sample_table.csv", key(KPP5) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people (lnpopulation = Z) if lnEducation12 != . , abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa1) all
insert_into_file using "sample_table.csv", key(KPNP1) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people (lnpopulation = Z) lnE12, abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa2) all
store_est_tpl using "sample_table.csv", coef(lnE12) name(esNPb2) all
insert_into_file using "sample_table.csv", key(KPNP2) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people (lnpopulation = Z) lnBlack, abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa3) all
store_est_tpl using "sample_table.csv", coef(lnBlack) name(esNPc3) all
insert_into_file using "sample_table.csv", key(KPNP3) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people (lnpopulation = Z) lnAge2, abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa4) all
store_est_tpl using "sample_table.csv", coef(lnAge2) name(esNPd4) all
insert_into_file using "sample_table.csv", key(KPNP4) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people (lnpopulation = Z) lnE12 lnBlack lnAge2, abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa5) all
store_est_tpl using "sample_table.csv", coef(lnE12) name(esNPb5) all
store_est_tpl using "sample_table.csv", coef(lnBlack) name(esNPc5) all
store_est_tpl using "sample_table.csv", coef(lnAge2) name(esNPd5) all
insert_into_file using "sample_table.csv", key(KPNP5) value(`e(rkf)') format(%5.2f)

gen miss_E = 1 if employed_mig == . | employed_nat ==. | employed_LS == . | employed_MS == . | employed_HS == . 

ivreghdfe lnpecuniary_V1  (lnpopulation=IV) if miss_E != 1 , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa6) all
insert_into_file using "sample_table.csv", key(KPP6) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV) employed if miss_E != 1, cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa7) all
store_est_tpl using "sample_table.csv", coef(employed) name(esPb7) all
insert_into_file using "sample_table.csv", key(KPP7) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV) employed_nat employed_mig if miss_E != 1 , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa8) all
store_est_tpl using "sample_table.csv", coef(employed_nat) name(esPb8) all
store_est_tpl using "sample_table.csv", coef(employed_mig) name(esPc8) all
insert_into_file using "sample_table.csv", key(KPP8) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV) employed_LS employed_MS employed_HS if miss_E != 1 , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa9) all
store_est_tpl using "sample_table.csv", coef(employed_LS) name(esPb9) all
store_est_tpl using "sample_table.csv", coef(employed_MS) name(esPc9) all
store_est_tpl using "sample_table.csv", coef(employed_HS) name(esPd9) all
insert_into_file using "sample_table.csv", key(KPP9) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV) if miss_E != 1 , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa6) all
insert_into_file using "sample_table.csv", key(KPNP6) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV) employed if miss_E != 1, cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa7) all
store_est_tpl using "sample_table.csv", coef(employed) name(esNPb7) all
insert_into_file using "sample_table.csv", key(KPNP7) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV) employed_nat employed_mig if miss_E != 1 , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa8) all
store_est_tpl using "sample_table.csv", coef(employed_nat) name(esNPb8) all
store_est_tpl using "sample_table.csv", coef(employed_mig) name(esNPc8) all
insert_into_file using "sample_table.csv", key(KPNP8) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV) employed_LS employed_MS employed_HS if miss_E != 1 , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa9) all
store_est_tpl using "sample_table.csv", coef(employed_LS) name(esNPb9) all
store_est_tpl using "sample_table.csv", coef(employed_MS) name(esNPc9) all
store_est_tpl using "sample_table.csv", coef(employed_HS) name(esNPd9) all
insert_into_file using "sample_table.csv", key(KPNP9) value(`e(rkf)') format(%5.2f)

gen miss_I = 1 if lnincome_mig == . | lnincome_nat ==. | lnincome_LS == . | lnincome_MS == . | lnincome_HS == . 

ivreghdfe lnpecuniary_V1  (lnpopulation=IV) if miss_I != 1 , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa10) all
insert_into_file using "sample_table.csv", key(KPP10) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV) lnincome if miss_I != 1, cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa11) all
store_est_tpl using "sample_table.csv", coef(lnincome) name(esPb11) all
insert_into_file using "sample_table.csv", key(KPP11) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV) lnincome_nat lnincome_mig  if miss_I != 1 , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa12) all
store_est_tpl using "sample_table.csv", coef(lnincome_nat) name(esPb12) all
store_est_tpl using "sample_table.csv", coef(lnincome_mig) name(esPc12) all
insert_into_file using "sample_table.csv", key(KPP12) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1  (lnpopulation=IV) lnincome_LS lnincome_MS lnincome_HS if miss_I != 1 , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa13) all
store_est_tpl using "sample_table.csv", coef(lnincome_LS) name(esPb13) all
store_est_tpl using "sample_table.csv", coef(lnincome_MS) name(esPc13) all
store_est_tpl using "sample_table.csv", coef(lnincome_HS) name(esPd13) all
insert_into_file using "sample_table.csv", key(KPP13) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV) if miss_I != 1 , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa10) all
insert_into_file using "sample_table.csv", key(KPNP10) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV) lnincome if miss_I != 1, cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa11) all
store_est_tpl using "sample_table.csv", coef(lnincome) name(esNPb11) all
insert_into_file using "sample_table.csv", key(KPNP11) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV) lnincome_nat lnincome_mig  if miss_I != 1 , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa12) all
store_est_tpl using "sample_table.csv", coef(lnincome_nat) name(esNPb12) all
store_est_tpl using "sample_table.csv", coef(lnincome_mig) name(esNPc12) all
insert_into_file using "sample_table.csv", key(KPNP12) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people  (lnpopulation=IV) lnincome_LS lnincome_MS lnincome_HS if miss_I != 1 , cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa13) all
store_est_tpl using "sample_table.csv", coef(lnincome_LS) name(esNPb13) all
store_est_tpl using "sample_table.csv", coef(lnincome_MS) name(esNPc13) all
store_est_tpl using "sample_table.csv", coef(lnincome_HS) name(esNPd13) all
insert_into_file using "sample_table.csv", key(KPNP13) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1 (lnpopulation = Z) if S_Occup != ., abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa14) all
insert_into_file using "sample_table.csv", key(KPP14) value(`e(rkf)') format(%5.2f)

ivreghdfe lnpecuniary_V1 (lnpopulation = Z) S_Occup, abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esPa15) all
store_est_tpl using "sample_table.csv", coef(S_Occup) name(esPb15) all
insert_into_file using "sample_table.csv", key(KPP15) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people (lnpopulation = Z) if S_Occup != ., abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa14) all
insert_into_file using "sample_table.csv", key(KPNP14) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people (lnpopulation = Z) S_Occup, abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(esNPa15) all
store_est_tpl using "sample_table.csv", coef(S_Occup) name(esNPb15) all
insert_into_file using "sample_table.csv", key(KPNP15) value(`e(rkf)') format(%5.2f)

table_from_tpl, t(TPL_Mech_P.tex) r(sample_table.csv) o(T_Mech_P1.tex)


* Table H1: Robustness to temperature shocks at destination, IV Estimates
* -----------------------------------------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear

preserve  
use "${directory}/ES_shift.dta", clear 
rename O_province province 
rename O_municipality local_municipality
tempfile temp
save `temp'
restore 
merge 1:1 province local_municipality year using `temp' 
drop if _merge != 3 

ivreghdfe lnpecuniary_V1 (lnpopulation = IV) Xtemp, cluster(id3) abs(id3 year)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es1) all
store_est_tpl using "sample_table.csv", coef(Xtemp) name(c1) all
insert_into_file using "sample_table.csv", key(KP1) value(`e(rkf)') format(%5.2f)

ivreghdfe lnNonpecu_people (lnpopulation = IV) Xtemp, cluster(id3) abs(id3 year) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es2) all
store_est_tpl using "sample_table.csv", coef(Xtemp) name(c2) all
insert_into_file using "sample_table.csv", key(KP2) value(`e(rkf)') format(%5.2f)

table_from_tpl, t(TPL_IV2.tex) r(sample_table.csv) o(T_IV2.tex)


* Table H3: Crime reporting, OLS Estimates
* ----------------------------------------
use "${directory}/ES_TableH3.dta", clear 
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear

reg Pecuniary Migrant i.id3 i.year if F_NATIONALITY == 1,   cluster(id3) 
store_est_tpl using "sample_table.csv", coef(Migrant) name(es1) all
boottest Migrant, cluster(id3) bootcluster(id3) weight(webb) level(95) seed(1) nograph
return list
insert_into_file using "sample_table.csv", key(p1) value(`r(p)') format(%5.2f)
sum Pecuniary, det 
insert_into_file using "sample_table.csv", key(M1) value(`r(mean)') format(%5.2f)

reg Report_Pecuniary Migrant i.id3 i.year if F_NATIONALITY == 1,   cluster(id3) 
store_est_tpl using "sample_table.csv", coef(Migrant) name(es2) all
boottest Migrant, cluster(id3) bootcluster(id3) weight(webb) level(95) seed(1) nograph
return list
insert_into_file using "sample_table.csv", key(p2) value(`r(p)') format(%5.2f)
sum Report_Pecuniary, det 
insert_into_file using "sample_table.csv", key(M2) value(`r(mean)') format(%5.2f)

table_from_tpl, t(TPL_VCS.tex) r(sample_table.csv) o(T_VCS2.tex) 



* Table H4: District level regressions, IV estimates
* --------------------------------------------------
use "${directory}/ES_TableH4.dta", clear 
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear

reghdfe lnpecuniary_V1 lnpopulation, abs(year) cluster(id2)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es1) all

reghdfe lnpecuniary_V1 lnpopulation, abs(id2) cluster(id2)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es2) all

reghdfe lnpecuniary_V1 lnpopulation, abs(id2 year) cluster(id2)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es3) all

ivreghdfe lnpecuniary_V1 (lnpopulation = IV_district), abs(id2 year) cluster(id2) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es4) all
insert_into_file using "sample_table.csv", key(KP4) value(`e(rkf)') format(%5.2f)

reghdfe lnNonpecu_people lnpopulation, abs(year) cluster(id2)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es5) all

reghdfe lnNonpecu_people lnpopulation, abs(id2) cluster(id2)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es6) all

reghdfe lnNonpecu_people lnpopulation, abs(id2 year) cluster(id2)
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es7) all

ivreghdfe lnNonpecu_people (lnpopulation = IV_district), abs(id2 year) cluster(id2) 
store_est_tpl using "sample_table.csv", coef(lnpopulation) name(es8) all
insert_into_file using "sample_table.csv", key(KP8) value(`e(rkf)') format(%5.2f)

table_from_tpl, t(TPL_Regdistrict.tex) r(sample_table.csv) o(T_Regdistrict.tex)


* Table I1: Plausibly Exogenous, OLS Estimates
* --------------------------------------------
use "${directory}/ES_db.dta", clear  
cd "${tables}"
cap erase "${tables}/sample_table.csv"
estimates clear

reghdfe lnpecuniary_V1 IV, abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(IV) name(es1) all
capture drop yres 
reg  lnpecuniary_V1 i.year i.id3
predict yres, res 
capture drop popres
reg  lnpopulation i.year i.id3
predict popres, res 
capture drop IVres
reg  IV i.year i.id3
predict IVres, res 

plausexog uci yres (popres = IVres), gmin(-0.004) gmax(0) grid(2) level(.95) vce(cluster id3)
insert_into_file using "sample_table.csv", key(b_lb_pec) value(`e(lb_popres)') format(%5.2f)
insert_into_file using "sample_table.csv", key(b_ub_pec) value(`e(ub_popres)') format(%5.2f)
plausexog uci yres (popres = IVres), gmin(-0.0014) gmax(0) grid(2) level(.95) vce(cluster id3) // 33% of the total effect. 
insert_into_file using "sample_table.csv", key(delta_m_pec) value(-0.0014) format(%5.4f)

reghdfe lnNonpecu_people IV, abs(id3 year) cluster(id3)
store_est_tpl using "sample_table.csv", coef(IV) name(es2) all
capture drop yres 
reg  lnNonpecu_people i.year i.id3
predict yres, res 
capture drop popres
reg  lnpopulation i.year i.id3
predict popres, res 
capture drop IVres
reg  IV i.year i.id3
predict IVres, res 

plausexog uci yres (popres = IVres), gmin(-0.001) gmax(0) grid(2) level(.95) vce(cluster id3)
insert_into_file using "sample_table.csv", key(b_lb_npec) value(`e(lb_popres)') format(%5.2f)
insert_into_file using "sample_table.csv", key(b_ub_npec) value(`e(ub_popres)') format(%5.2f)
plausexog uci yres (popres = IVres), gmin(-0.005) gmax(-0.004) grid(2) level(.95) vce(cluster id3) // 33% of the total effect. 
insert_into_file using "sample_table.csv", key(delta_m_npec) value("X") format(%5.4f)

table_from_tpl, t(TPL_IV_Conley.tex) r(sample_table.csv) o(T_IV_Conley.tex)


* Figure J1: Simulated pecuniary crime variable, IV Estimates
* -----------------------------------------------------------
forvalues r = 0/10 {
	forvalues s=0/10 {
preserve 
xtset id3 year 
sort id3 year 
bysort id3: gen delta_population = population - l.population
gen Native = population if year == 2011 
bysort id3: replace Native = Native[_n-1] if year != 2011
gen Migrants  = delta_population if delta_population > 0
bysort id3: gen Migrants_stock = sum(Migrants) 
gen Victimization_N0 = pecuniary_V1/population if year == 2011 
bysort id3: replace Victimization_N0 = Victimization_N0[_n-1] if year != 2011

gen Reporting_N = 1
gen Reporting_M = `r'/10*Reporting_N
gen shift = `s'/10
gen Victimization_N = Victimization_N0 
replace Victimization_N = Victimization_N0*((Native-shift*Migrants_stock)/Native) if year == 2012 & delta_population > 0
bysort id3: replace Victimization_N = Victimization_N[_n-1]*((Native-shift*Migrants_stock)/Native) if year > 2012 & delta_population > 0

gen Victimization_M = Victimization_N0
bysort id3: replace Victimization_M = Victimization_N0*(1+(shift*Migrants_stock/Native)) if year == 2012
bysort id3: replace Victimization_M = Victimization_N0*(1+(shift*Migrants_stock/Native)) if year > 2012

gen Crime_pc = asinh((Native*Victimization_N*Reporting_N + Migrants_stock*Victimization_M*Reporting_M) /population*100000)
bysort id3: replace Crime_pc = Crime_pc[_n-1] if year != 2011 & delta_population < 0 

ivreghdfe Crime_pc (lnpopulation = IV) , abs(id3 year) cluster(id3)
quietly parmest,format(estimate min95 max95 %8.3f p %8.3f) list(,)   norestore 
keep parm stderr estimate p  
keep in 1
gen r = `r'  
gen s = `s' 
save  "${simulations}/est_r`r'_s`s'.dta", replace 
restore 
	}
}

clear all 
cd "${simulations}/"
forvalues r = 0/10 {
	forvalues s=0/10 {
append using  est_r`r'_s`s'  
	}
}

gen ttest = (estimate+1.9)/stderr
replace r= r/10
replace s= s/10
replace estimate=. if ttest < 1.96
replace estimate = round(estimate, 0.01)
heatplot estimate r s, cut(@min(.2)@max)  colors(greens,reverse) graphregion(fcolor(white)) 
graph export "${figures}/F_simulations.jpg", as(jpg) name("Graph") quality(100) replace 

twoway (contour estimate r s, graphregion(fcolor(white)) ccuts(-3(0.5) 0) crule(intensity) ecolor(blue)  zlabel(, format(%8.2f)  labsize(small))   yscale(axis(1) alt) xtitle(Under-Reporting, size(small))  ytitle(Shift, size(small)) title(`base' , )  heatmap ztitle("{&beta}{subscript:m}") )  